/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.datepicker;

import java.util.Date;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.TextField;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Component used to enable date select on inputs<br>
 * Jquery use for this behavior is <code>$('#id').datepicker(options);</code><br>
 * With Wicket, you just need to create a JQDatePicker Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQDatePicker extends WebMarkupContainer implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQDatePickerPlugin plugin;
	/** JQuery default options */
	private JQDatePickerOptions options = new JQDatePickerOptions();

	/** disabled */
	private boolean disabled = false;
	/** start date */
	private Date startDate;
	/** end date */
	private Date endDate;

	/** Date container */
	private TextField parent;

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component
	 * @param key
	 *            : plugin unique id
	 * @param parent
	 *            parent textfield
	 */
	public JQDatePicker(String id, String key, TextField parent)
	{
		super(id);
		this.parent = parent;
		this.parent.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQDatePickerPlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQDatePickerPlugin(key);
		}
		this.plugin = (JQDatePickerPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component
	 * @param parent
	 *            parent textfield
	 */
	public JQDatePicker(String id, TextField parent)
	{
		this(id, JQDatePickerPlugin.DEFAULT_KEY, parent);
	}

	public JQDatePickerOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQDatePickerOptions.class))
		{
			this.options = (JQDatePickerOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQDatePicker.class, options.getClass(), JQDatePickerOptions.class);
		}
	}

	public JQDatePickerPlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQDatePickerPlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.parent.getMarkupId() + "')." + (reset ? "datepicker('destroy')." : "") + this.getOptions().getDefaultHeader() + "($.extend({}, $.datepicker.regional" + this.plugin.getRegional() + ", "
				+ (JQDatePicker.this.getOptions().size() == 0 ? "{}" : JQDatePicker.this.getOptions().getDefaultString()) + "));";
	}

	public String getAfterAjaxUpdateScript()
	{
		return getWidgetReadyScript(true);
	}

	/**
	 * Applies new settings to this datepicker<br>
	 * Appends a javascript call <code>$('#id').draggable('change', {settings})</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 * @param settings
	 *            datepicker's settings
	 */
	public void change(AjaxRequestTarget target, JQDatePickerOptions settings)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('change', " + settings.getDefaultString() + ")");
	}

	/**
	 * Shows a datepicker in a dialog<br>
	 * Appends a javascript call <code>$('#id').draggable('dialog', date, onSelect:function(){}, {settings})</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 * @param startDate
	 *            startDate for this datepicker
	 * @param onSelectScript
	 *            on date select callback
	 * @param settings
	 *            datepicker's settings
	 */
	public void dialog(AjaxRequestTarget target, Date startDate, String onSelectScript, JQDatePickerOptions settings)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('dialog', " + startDate + ", function(e, ui){" + onSelectScript + "}, " + settings.getDefaultString() + ")");
	}

	/**
	 * Disables this datepicker<br>
	 * Appends a javascript call <code>$('#id').draggable('disable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void disable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('disable')");
		this.disabled = true;
	}

	/**
	 * Enables this datepicker<br>
	 * Appends a javascript call <code>$('#id').draggable('enable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void enable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('enable')");
		this.disabled = false;
	}

	/**
	 * Gets if this datepicker is disabled or not
	 * 
	 * @return this datepicker's disabled status
	 */
	public boolean isDisabled()
	{
		return disabled;
	}

	/**
	 * Hides this datepicker<br>
	 * Appends a javascript call <code>$('#id').draggable('hide', speed)</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 * @param speed
	 *            speed for hide animation
	 */
	public void hide(AjaxRequestTarget target, int speed)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('hide', " + speed + ")");
	}

	/**
	 * Shows this datepicker<br>
	 * Appends a javascript call <code>$('#id').draggable('show')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void show(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('show')");
	}

	/**
	 * Destroys this datepicker<br>
	 * Appends a javascript call <code>$('#id').draggable('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('destroy')");
	}

	/**
	 * Gets this datepicker's start date
	 * 
	 * @return the datepicker's start date
	 */
	public Date getStartDate()
	{
		return startDate;
	}

	protected void setStartDate(Date startDate)
	{
		this.startDate = startDate;
	}

	/**
	 * Gets this datepicker's end date, only available if range select is enable
	 * 
	 * @return the datepicker's end date
	 */
	public Date getEndDate()
	{
		return endDate;
	}

	protected void setEndDate(Date endDate)
	{
		this.endDate = endDate;
	}

	/**
	 * Sets this datepicker's date<br>
	 * Appends a javascript call <code>$('#id').draggable('setDate', date)</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 * @param startDate
	 *            datepicker's new date
	 */
	public void setDate(Date startDate, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('setDate', " + startDate + ")");
		this.startDate = startDate;
	}

	/**
	 * Sets this datepicker's dates<br>
	 * Appends a javascript call <code>$('#id').draggable('setDate', startDate, endDate)</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 * @param startDate
	 *            datepicker's new start date
	 * @param endDate
	 *            datepicker's new end date
	 */
	public void setDate(Date startDate, Date endDate, AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + parent.getMarkupId() + "').datepicker('setDate', " + startDate + "," + endDate + ")");
		this.startDate = startDate;
		this.endDate = endDate;
	}

}
